import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

/**
 * represents the Store where MULEs and Resources can be bought by different Player objects
 * located inside of the Town
 * @author Sammy Warrayat
 *
 */
public class Store extends JPanel{
	
	private InventoryResource[] inventory;		//the store's inventory
	private JLabel storeStatus = new JLabel();	//text representing the status of the store
	
	/**
	 * creates an instance of a store
	 */
	public Store(){
		inventory = new InventoryResource[6];
		//follows playerResources order in Player
		inventory[0] = new InventoryResource("food");
		inventory[1] = new InventoryResource("energy");
		inventory[2] = new InventoryResource("smithOre");
		inventory[3] = new InventoryResource("muleFood");
		inventory[4] = new InventoryResource("muleEnergy");
		inventory[5] = new InventoryResource("muleSmithOre");
	}
	
	/**
	 * checks if user has enough money
	 * @param itemNum the number corresponding to the item
	 * @param p the Player
	 * @return boolean if the Player can afford the item
	 */
	public boolean canAfford(int itemNum, Player p){
		return inventory[itemNum].getPrice() <= p.getMoney();
	}
	
	/**
	 * attempts to sell a resource for a player
	 * @param itemNumber the resource being sold
	 * @param p the player selling the resource
	 * @return whether or not the resource was sold
	 */
	public boolean sell(int itemNumber, Player p){
		if(itemNumber < 0 || itemNumber > 5 || p == null){
			return false;
		}
		int amountOwned = p.getPlayerResource(itemNumber);
		//check if player has enough of the resource
		if (amountOwned > 0){
			//show that player has sold the item
			storeStatus.setText(p.getName() + " sold " + inventory[itemNumber].getName());
			//update player resources
			p.setPlayerResource(itemNumber, amountOwned - 1);
			//give player his money
			p.setMoney(p.getMoney() + inventory[itemNumber].getPrice());
			//add resource to inventory
			int storeQuantity = inventory[itemNumber].getAmount();
			inventory[itemNumber].setAmount(storeQuantity + 1);
			return true;
		}
		else{
			//player doesn't have anymore
			storeStatus.setText(p.getName() + " does not have anymore " + inventory[itemNumber].getName());
			return false;
		}
	}
	
	/**
	 * attempts to buy a resource for the given player
	 * @param p the player buying the resource
	 * @param i the item being bought
	 * @return whether or not the item was bought
	 */
	public boolean buy(Player p, int i){
		if(inventory[i].getAmount() <= 0 || p.getMoney() < inventory[i].getPrice()){
			return false;
		}
		//subtract money from player
		int newMoney = p.getMoney() - inventory[i].getPrice();
		p.setMoney(newMoney);
		//subtract from inventory
		inventory[i].setAmount(inventory[i].getAmount() - 1);
		//add to player inventory
		p.setPlayerResource(i, p.getPlayerResource(i) + 1);
		//return that the purchase was successful
		return true;
	}
	
	/**
	 * gets the current inventory
	 * @return the inventory
	 */
	public InventoryResource[] getInventory(){
		return inventory;
	}
	

}
